package pack1;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.swim.entity.SkillRequest;
import com.swim.session.NewSkillRequestRemote;
import com.swim.session.SkillRemote;

public class NewSkillServlet extends HttpServlet {
	
	private static final long serialVersionUID = 8811132527636791747L;

	public NewSkillServlet() {
	        super();
	    }
	  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
    	if(session.getAttribute("authenticated") == null || !(boolean)session.getAttribute("authenticated")) {
			response.sendRedirect("index.jsp");
			return;
    	}
    	showNewSkillForm(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
    	if(session.getAttribute("authenticated") == null || !(boolean)session.getAttribute("authenticated")) {
			response.sendRedirect("index.jsp");
			return;
    	}
    	showNewSkillForm(request, response);
	}
	
	private void showNewSkillForm(HttpServletRequest request, HttpServletResponse response) {


    	String idStr = request.getParameter("id");
    	String name = request.getParameter("name");
    	String desc = request.getParameter("desc");

		try {
		if(idStr == null) {
			InitialContext jndiContext = (InitialContext) Util.getInitialContext();

			Object ref1 = jndiContext.lookup("SkillBeanJNDI");
			SkillRemote r = (SkillRemote)ref1;
			if(name != null && desc != null) {
				r.newSkill(name, desc);
				request.setAttribute("title","Skill added");
				request.setAttribute("message","The skill has been added. You can go <a href=\"index.jsp\">back</a> to the homepage.");
				RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
				dispatcher.forward(request, response);
			}
			
		} else {
				InitialContext jndiContext = (InitialContext) Util.getInitialContext();
				
				Object ref = jndiContext.lookup("NewSkillRequestBeanJNDI");
				int id = Integer.parseInt(idStr);
				if(name == null || desc == null){
					SkillRequest r = ((NewSkillRequestRemote)ref).getSkillRequest(id);
					request.setAttribute("nsreq", r);
				} else {
					((NewSkillRequestRemote)ref).acceptNewSkillRequest(id, true, name, desc);

					request.setAttribute("title","Skill added");
					request.setAttribute("message","The skill has been added. You can go <a href=\"index.jsp\">back</a> to the homepage.");
					RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
					dispatcher.forward(request, response); 
					return;
				}
			}
		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/auth/newskill.jsp");
		dispatcher.forward(request, response); 
		}catch(Exception e) {}
	}
}
